package com.iiapk.remind.server.domain.mail;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.log4j.Logger;

import com.iiapk.core.DateUtils;

/**
 * 邮件抽象,表示一个具体的邮件内容,一个提醒最多可以实现3次邮件 
 * @author Atomic
 */
public class MailEntity implements Serializable {

	private static final long serialVersionUID = -5476694965258044689L;

	Logger logger = Logger.getLogger("~~com.iiapk.entitydebugger.MAILEntity~~");

	private int id; // id
	private Date sendDate; //发送时间
	private String context; //邮件的数据
	private String target;//发送目的地

	private int remindId;//该邮件是隶属于那个提醒

	private HashMap<String, String> data = new HashMap<String, String>(5); //为了提供邮件发送时的模板设置,从context格式化过来的map

	/**
	 * 提供给Velocity中进行内容构造时的数据们 
	 */
	public HashMap<String, String> getData() {

		String _c = StringUtils.substringAfter(context, "email://");

		for (String _s : StringUtils.split(_c, ";")) {
			String[] _s1 = StringUtils.split(_s, ":");
			if (_s1 == null || _s1.length != 2) {
				logger.error("[%s]的提醒[%s]数据异常!");
			}
			data.put(_s1[0], _s1[1]);
		}
		//计算额外的数据,剩余天数等

		Date birthday = null;
		try {
			birthday = new SimpleDateFormat("yyyy-MM-dd").parse(data.get("birthday"));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar c = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		c.setTime(birthday);
		c.set(Calendar.YEAR, now.get(Calendar.YEAR));

		int rest = DateUtils.getDaysBetween(now, c);
		
		data.put("rest", String.valueOf(rest));

		return data;
	}

	public MailEntity() {

	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public int getRemindId() {
		return remindId;
	}

	public void setRemindId(int remindId) {
		this.remindId = remindId;
	}

	public Date getSendDate() {
		return sendDate;
	}

	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

}
